﻿using System;
using System.Text;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using Tssipl.PaymentAdapter.Core;
namespace Tssipl.PaymentAdapter.MessageFormat.Swift
{

    public class TestSwiftParser  //HACK extends TestCase
    {
        static private String GoodSwiftMT202Message = "{1:F01ABCDUS10AXXX0000000000}{2:I202MNOPUS40HXYZX}{4:\r\n" +
                ":20:101101012345678A\r\n" +
                ":21:A10112090QQ1-150\r\n" +
                ":32A:101101USD10100,50\r\n" +
                ":52A:ABCDCHAAXXX\r\n" +
                ":58A:QRSTJP90MNO\r\n" +
                ":72:/REC//DEDUCT/\r\n" +
                "-}{5:{MAC:41720873}{CHK:123456789ABC}}";

        static private String GoodSwiftMT541Message = "{1:F01ABCDUS20AXXX0000000000}{2:I541MNOPHK50XABCN}{3:{108:101101C9010F99}}{4:\r\n" +
                ":16R:GENL\r\n" +
                ":20C::SEME//101101C9010F99\r\n" +
                ":23G:NEWM\r\n" +
                ":16S:GENL\r\n" +
                ":16R:TRADDET\r\n" +
                ":98A::SETT//20101101\r\n" +
                ":98A::TRAD//20101101\r\n" +
                ":90A::DEAL//PRCT/96,571\r\n" +
                ":35B:ISIN US100001AB10\r\n" +
                "BLAH1 PROPERTY GROUP INC\r\n" +
                ":16S:TRADDET\r\n" +
                ":16R:FIAC\r\n" +
                ":36B::SETT//FAMT/12345,\r\n" +
                ":97A::SAFE//FUN03\r\n" +
                ":16S:FIAC\r\n" +
                ":16R:SETDET\r\n" +
                ":22F::SETR//TRAD\r\n" +
                ":16R:SETPRTY\r\n" +
                ":95P::PSET//ABCDGBAA\r\n" +
                ":16S:SETPRTY\r\n" +
                ":16R:SETPRTY\r\n" +
                ":95R::SELL/ECLR/11996\r\n" +
                ":16S:SETPRTY\r\n" +
                ":16R:SETPRTY\r\n" +
                ":95R::DEAG/ECLR/11996\r\n" +
                ":16S:SETPRTY\r\n" +
                ":16R:AMT\r\n" +
                ":19A::SETT//USD12345,98\r\n" +
                ":16S:AMT\r\n" +
                ":16S:SETDET\r\n" +
                "-}";

        static private String GoodSwiftMT191Message = "{1:F01MNOPUS10AXXX1684207429}{2:O1910953101130WXYZHK10AXXX36030001231011300045N}{4:\r\n" +
                ":20:123456780123\r\n" +
                ":21:2010113000000199\r\n" +
                ":32B:USD45,\r\n" +
                ":57A:ABCDUS20XXX\r\n" +
                ":71B:/COMM/\r\n" +
                "-}{5:{CHK:880A550C6F00}{MAC:00000000}}";


        public void testMT202() //throws Exception
        {

            MessageParseResults results = SwiftMessageParser.parseMessage(GoodSwiftMT202Message);

            //assertNotNull(results.getMessage());

            ////SwiftMessage message = (SwiftMessage)results.getMessage();
            ////Assert.AreEqual(message.getBlock1().getBIC12(), "ABCDUS10AXXX");

            ////Assert.AreEqual(message.getBlock1().getBIC11(), "ABCDUS10XXX");
            ////Assert.AreEqual(message.getBlock1().getBIC8(), "ABCDUS10");

            ////SwiftBlock2Inbound block2 = message.getBlock2Inbound();
            ////assertNotNull(block2);
            ////assertNull(message.getBlock2Outbound());

            ////Assert.AreEqual(message.getMT(), "202");

            ////Assert.AreEqual(block2.getBIC12(), "MNOPUS40HXYZ");
            ////Assert.AreEqual(block2.getBIC11(), "MNOPUS40XYZ");
            ////Assert.AreEqual(block2.getBIC8(), "MNOPUS40");

            ////Assert.AreEqual(message.getBlock4().getFields().Count, 6);

            ////Assert.AreEqual(GoodSwiftMT202Message, message.ToString());

            ////Assert.AreEqual(message.getBlock4().getField("20"), "101101012345678A");
            ////Assert.AreEqual(message.getBlock4().getField("21"), "A10112090QQ1-150");
            ////Assert.AreEqual(message.getBlock4().getField("58A"), "QRSTJP90MNO");
        }

        public void testMT541()// throws Exception
        {
            MessageParseResults results = SwiftMessageParser.parseMessage(GoodSwiftMT541Message);
            if (results == null) return;
            //        assertNotNull(results.getMessage());
            //Assert.IsNotNull(results.getMessage()); 
            SwiftMessage message = (SwiftMessage)results.getMessage();
            //Assert.AreEqual(message.getBlock1().getBIC12(), "ABCDUS20AXXX");
            //Assert.AreEqual(message.getBlock1().getBIC11(), "ABCDUS20XXX");
            //Assert.AreEqual(message.getBlock1().getBIC8(), "ABCDUS20");

            SwiftBlock2Inbound block2 = message.getBlock2Inbound();
            //HACK assertNotNull(block2);
            //Assert.IsNotNull(block2);      
            //assertNull(message.getBlock2Outbound());
            //Assert.IsNull(message.getBlock2Outbound());

            //Assert.AreEqual(message.getMT(), "541");

            //Assert.AreEqual(block2.getBIC12(), "MNOPHK50XABC");
            //Assert.AreEqual(block2.getBIC11(), "MNOPHK50ABC");
            //Assert.AreEqual(block2.getBIC8(), "MNOPHK50");
            //----------------		
            //Assert.AreEqual(message.getBlock4().getFields().Count, 29);

            //Assert.AreEqual(GoodSwiftMT541Message, message.ToString());

            //Assert.AreEqual(message.getBlock4().getField("20C"), ":SEME//101101C9010F99");
            //Assert.AreEqual(message.getBlock4().getField("23G"), "NEWM");
            String[] fields = message.getBlock4().getFields("16R");
            //Assert.AreEqual(fields.Length, 8);
            //Assert.AreEqual(fields[0], "GENL");
            //Assert.AreEqual(fields[1], "TRADDET");
            //Assert.AreEqual(fields[5], "SETPRTY");
            //Assert.AreEqual(fields[7], "AMT");
        }

        public void testMT191()// throws Exception
        {
            MessageParseResults results = SwiftMessageParser.parseMessage(GoodSwiftMT191Message);
            //assertNotNull(results.getMessage());

            //SwiftMessage message = (SwiftMessage)results.getMessage();
            //Assert.AreEqual(message.getBlock1().getBIC12(),"MNOPUS10AXXX");
            //Assert.AreEqual(message.getBlock1().getBIC11(),"MNOPUS10XXX");
            //Assert.AreEqual(message.getBlock1().getBIC8(),"MNOPUS10");

            //SwiftBlock2Outbound block2 = message.getBlock2Outbound();
            //assertNotNull(block2);
            //assertNull(message.getBlock2Inbound());

            //Assert.AreEqual(message.getMT(),"191");

            //Assert.AreEqual(block2.getBIC12(),"WXYZHK10AXXX");
            //Assert.AreEqual(block2.getBIC11(),"WXYZHK10XXX");
            //Assert.AreEqual(block2.getBIC8(),"WXYZHK10");

            //Assert.AreEqual(block2.getSequence(),"000123");
            //Assert.AreEqual(block2.getOutputDate(),"101130");
            //Assert.AreEqual(block2.getInputTime(),"0953");
            //Assert.AreEqual(block2.getSessionNo(),"3603");

            //Assert.AreEqual(message.getBlock4().getFields().Count,5);

            //Assert.AreEqual(GoodSwiftMT191Message,message.ToString());

            //Assert.AreEqual(message.getBlock4().getField("20"),"123456780123");
            //Assert.AreEqual(message.getBlock4().getField("21"),"2010113000000199");
            //Assert.AreEqual(message.getBlock4().getField("32B"),"USD45,");
        }

        public void testMT103()// throws Exception
        {
            String GoodSwiftMT103Message = "{1:F01MIDLGB22AXXX0548034693}{2:I103BKTRUS33XBRDN3}{3:{108:MT103}}{4:\r\n" +
                ":20:8861198-0706\r\n" +
                ":23B:CRED\r\n" +
                ":32A:000612USD5443,99\r\n" +
                ":33B:USD5443,99\r\n" +
                ":50K:GIAN ANGELO IMPORTS\r\n" +
                "NAPLES\r\n" +
                ":52A:BCITITMM500\r\n" +
                ":53A:BCITUS33\r\n" +
                ":54A:IRVTUS3N\r\n" +
                ":57A:BNPAFRPPGRE\r\n" +
                ":59:/20041010050500001M02606\r\n" +
                "KILLY S.A.\r\n" +
                "GRENOBLE\r\n" +
                ":70:/RFB/INVOICE 559661\r\n" +
                ":71A:SHA\r\n" +
                "-}";

            MessageParseResults results = SwiftMessageParser.parseMessage(GoodSwiftMT103Message);
            if (results == null) return;
            //        assertNotNull(results.getMessage());
            //Assert.IsNotNull(results.getMessage()); 
            SwiftMessage message = (SwiftMessage)results.getMessage();
            //Assert.AreEqual(message.getBlock1().getBIC12(), "ABCDUS20AXXX");
            //Assert.AreEqual(message.getBlock1().getBIC11(), "ABCDUS20XXX");
            //Assert.AreEqual(message.getBlock1().getBIC8(), "ABCDUS20");

            SwiftBlock2Inbound block2 = message.getBlock2Inbound();
            //HACK assertNotNull(block2);
            //Assert.IsNotNull(block2);      
            //assertNull(message.getBlock2Outbound());
            //Assert.IsNull(message.getBlock2Outbound());

            //Assert.AreEqual(message.getMT(), "541");

            //Assert.AreEqual(block2.getBIC12(), "MNOPHK50XABC");
            //Assert.AreEqual(block2.getBIC11(), "MNOPHK50ABC");
            //Assert.AreEqual(block2.getBIC8(), "MNOPHK50");
            //----------------		
            //Assert.AreEqual(message.getBlock4().getFields().Count, 29);

            //Assert.AreEqual(GoodSwiftMT541Message, message.ToString());

            //Assert.AreEqual(message.getBlock4().getField("20C"), ":SEME//101101C9010F99");
            //Assert.AreEqual(message.getBlock4().getField("23G"), "NEWM");
            String[] fields = message.getBlock4().getFields("16R");
            //Assert.AreEqual(fields.Length, 8);
            //Assert.AreEqual(fields[0], "GENL");
            //Assert.AreEqual(fields[1], "TRADDET");
            //Assert.AreEqual(fields[5], "SETPRTY");
            //Assert.AreEqual(fields[7], "AMT");
        }
    }
}